import {Injectable} from '@angular/core';
import {baseHandleError, BaseService, ReturnForm} from '@sb/base';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {catchError} from 'rxjs/operators';
import {OrgRole} from '../entity/org-role';
import {OrgRoleSearchForm} from '../entity/org-role-search-form';

/**
 * 机构角色服务
 */
@Injectable({
    providedIn: 'root'
})
export class OrgRoleService extends BaseService<OrgRole, OrgRoleSearchForm> {


    readonly basePath = 'permission/orgRole';

    constructor(
        private http: HttpClient
    ) {
        super(http);
    }

    listByRoleId(roleId: string): Observable<ReturnForm<OrgRole[]>> {
        return this.http.post<ReturnForm<OrgRole[]>>(`${this.url}/listByRoleId`, {
            roleId: roleId
        }).pipe(
            catchError(baseHandleError<ReturnForm<OrgRole[]>>('listByRoleId'))
        );
    }

    listByOrgId(orgId: string): Observable<ReturnForm<OrgRole[]>> {
        return this.http.post<ReturnForm<OrgRole[]>>(`${this.url}/listByOrgId`, {
            orgId: orgId
        }).pipe(
            catchError(baseHandleError<ReturnForm<OrgRole[]>>('listByOrgId'))
        );
    }

    commitByOrgId(roleIds: string[], orgId: string): Observable<ReturnForm<string>> {
        return this.http.post<ReturnForm<string>>(`${this.url}/commitByOrgId`, {
            roleIds: roleIds,
            orgId: orgId
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('commitByOrgId'))
        );
    }

    commitByRoleId(roleId: string, orgIds: string[]): Observable<ReturnForm<string>> {
        return this.http.post<ReturnForm<string>>(`${this.url}/commitByRoleId`, {
            roleId: roleId,
            orgIds: orgIds
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('commitByRoleId'))
        );
    }


    removeByOrgId(roleIds: string[], orgId: string): Observable<ReturnForm<string>> {
        return this.http.post<ReturnForm<string>>(`${this.url}/removeByOrgId`, {
            roleIds: roleIds,
            orgId: orgId
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('removeByOrgId'))
        );
    }

    removeByRoleId(roleId: string, orgIds: string[]): Observable<ReturnForm<string>> {
        return this.http.post<ReturnForm<string>>(`${this.url}/removeByRoleId`, {
            roleId: roleId,
            orgIds: orgIds
        }).pipe(
            catchError(baseHandleError<ReturnForm<string>>('removeByRoleId'))
        );
    }
}
